package interfaz_usuario;


import interfaces.impl.Agent;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import factoryNCastle.*;


import objects.SpetialFeature;
import objects.WorldObject;


public class PopUpObjectMenu extends JPopupMenu{

	private WorldObject object;
	
	public PopUpObjectMenu(WorldObject object){
		super();
		this.object=object;
		
		JMenu agentMenu = new JMenu("Agregar Agente");
		for(Agent ag : AgentSingleton.listAgents){
			JMenuItem menu = new JMenuItem(ag.getName());
			menu.addActionListener(new setAgentClicked(ag.getID()));
			if(object.containsAgent(ag.getID())){
				menu.setBackground(Color.GREEN);
			}
			agentMenu.add(menu);
		}
		add(agentMenu);
		
		JMenu spetialFeaturesMenu = new JMenu("Carac Espe");
		
		for(SpetialFeature f : SpetialFeatureFactory.listSpetialFeatures){
			JMenuItem menu = new JMenuItem(f.getName());
			menu.addActionListener(new setSpetialFeatureClicked(f.getId()));
			if(object.containsSpetialFeature(f.getId())){
				menu.setBackground(Color.GREEN);
			}
			spetialFeaturesMenu.add(menu);
		}
		add(spetialFeaturesMenu);
		
	}
	
	private class setAgentClicked implements ActionListener{
		
		int agentReference;
		
		public setAgentClicked(int id) {
			agentReference=id;
		}

		@Override
		public void actionPerformed(ActionEvent a) {
			
			if(!object.containsAgent(agentReference)){
				AgentSingleton.setAgent(object, agentReference);
			}
			else{
				object.removeAgent(agentReference);
			}
			
		}
		
	}
	
	private class setSpetialFeatureClicked implements ActionListener{

		int spetialFeatureReference;
		
		public setSpetialFeatureClicked(int id) {
			spetialFeatureReference=id;
		}

		@Override
		public void actionPerformed(ActionEvent arg0) {
			if(!object.containsSpetialFeature(spetialFeatureReference)){
				object.addSpetialFeature(SpetialFeatureFactory.getSpetialFeature(spetialFeatureReference));
			}
			else{
				object.removeSpetialFeature(spetialFeatureReference);
				}
		}
		
	}
	
}
